package com.second.system.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.second.common.pojo.User;
import com.second.common.vo.Result;
import com.second.system.dao.UserDao;
import com.second.system.service.UserService;
import com.second.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: second-education-parent
 * @description:
 * @author: 王子朋
 * @create: 2023-05-22 19:20
 **/
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public User findByNameAndPwd(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        //把输入的密码加盐
//        wrapper.eq("password", DigestUtil.sha256Hex(password));
//        wrapper.eq("password",password);
        wrapper.eq("password",DigestUtil.md5Hex(password));

        User user = userDao.selectOne(wrapper);
        return user;
    }
    //注意:分页效果要想出线必须要有分页的配置类
    @Override
    public Page<User> findByConditionPage(Integer current, Integer pageSize, UserVo userVo) {
        Page<User> page= new Page<>(current,pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(userVo.getRealName())){
            wrapper.like("nick_name",userVo.getRealName());
        }
        Page<User> page1 = userDao.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setIsDeleted(status);
        user.setGmtModified(LocalDateTime.now());//jdk8的新特性 使用new date 会有线程安全问题 用LocalDate LocalDateTime
        int row = userDao.updateById(user);
        return row;
    }

    @Override
    public int update( String id,String oldPassword, String newPassword) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(id)){
            wrapper.eq("id",id);

        }
        if (StringUtils.hasText(oldPassword)){
            wrapper.eq("password",DigestUtil.md5Hex(oldPassword));
        }
        List<User> users = userDao.selectList(wrapper);
        if (users.size()==0){
            return 0;
        }

        User user = new User();
        user.setId(id);
        user.setPassword(DigestUtil.md5Hex(newPassword));
        user.setGmtModified(LocalDateTime.now());

        userDao.updateById(user);
        return 1;
    }

}
